﻿using System;
using System.Collections.Generic;
using System.Text;
using com.dz.ct.common;
using System.Text.RegularExpressions;

namespace com.dz.ct.components.BigSite.www.yqk.com
{
    public class BigSiteObject:IBigSiteObject
    {
        /// <summary>
        /// 获取首页所有的书籍
        /// </summary>
        /// <param name="i_book_default_url"></param>
        /// <returns></returns>
        public Dictionary<string, string> CollectDefaultBookList(string i_book_default_url)
        {
            return null;
        }
        ///http://all.17k.com/0_00_5_2_60_{0}_0_0.html
        /// <summary>
        /// 收集某个地址的所有小说详情地址
        /// </summary>
        /// <param name="i_book_list_url"></param>
        /// <returns>小说地址及小说名称</returns>
        public List< BookInfo> CollectBookList(string i_book_list_url)
        {
            string t_reg = "<td[^<>]*?class=[\'\"]*?td2[\'\"]*?>\\s*?<a[^<>]*?href=s*[\'\"]*([^\"\']*)[\'\"]*[^<>]*?>([^<>]*?)</a>\\s*?</td>\\s*?<td[^<>]*?>.*?</td>\\s*?<td[^<>]*?class=[\'\"]*?td4[\'\"]*?>([^<>]*?)</td>";

            List<BookInfo> t_list = null;
            BookInfo t_book = null;
            DateTime t_update_time=DateTime.Now;
            try
            {
                string t_html = NetSiteCatchManager.ReadUrl(i_book_list_url, Encoding.UTF8);
                string t_url = string.Empty;
                if (!string.IsNullOrEmpty(t_html))
                {
                    MatchCollection t_ma = Regex.Matches(t_html, t_reg, RegexOptions.IgnoreCase | RegexOptions.Compiled);
                    if (t_ma != null && t_ma.Count > 0)
                    {
                        t_list = new List<BookInfo>();

                        foreach (Match t_mc in t_ma)
                        {
                            if (t_mc.Groups.Count >=3)
                            {
                                t_book = new BookInfo();

                                t_book.BookName = t_mc.Groups[2].Value.ToString().Trim();
                                t_book.BookUrl = t_mc.Groups[1].Value.ToString().Trim();
                                if (!DateTime.TryParse(t_mc.Groups[3].Value.ToString().Trim(), out t_update_time))
                                    t_update_time = DateTime.Now;
                                t_book.LastUpdateTime = t_update_time;
                                t_list.Add(t_book);
                            }
                        }
                    }
                }

            }
            catch (Exception ex)
            {
                LogHelper.Error("收集小说列表地址失败" + ex.ToString());
            }

            return t_list;
        }

        /// <summary>
        /// 收集某本小说详细信息
        /// </summary>
        /// <param name="i_book_url"></param>
        /// <returns></returns>
        public BookInfo CollectBookItem(string i_book_url)
        {
            Uri t_url = new Uri(i_book_url);
            BookInfo t_book_info = null;
            try
            {
                string t_html = NetSiteCatchManager.ReadUrl(i_book_url, Encoding.UTF8);
                if (!string.IsNullOrEmpty(t_html))
                {
                    Match t_mc = null;
                     t_book_info = new BookInfo();
                     t_mc = Regex.Match(t_html, "<div[^<>]*?class=[\'\"]*?infoPath[\'\"]*?[^<>]*?><a[^<>]*?>.*?</a>\\s*?<a[^<>]*?>(.*?)</a>\\s*?<a[^<>]*?>(.*?)</a>\\s*?<span[^<>]*?>\\s*?<a[^<>]*?href=s*[\'\"]*([^\"\']*)[\'\"]*[^<>]*?>(.*?)</a>\\s*?</span>\\s*?<span[^<>]*?>.*?</span>\\s*?</div>", RegexOptions.Compiled | RegexOptions.IgnoreCase);
                  
                    if (t_mc != null && t_mc.Groups.Count > 3)
                    {
                        t_book_info.BookCategoryName = t_mc.Groups[1].Value.ToString();
                        t_book_info.BookSubCategroyName = t_mc.Groups[2].Value.ToString();
                        t_book_info.BookUrl = i_book_url;
                        t_book_info.BookChapterListUrl = string.Format("http://{0}{1}",t_url.Host, t_mc.Groups[3].Value.ToString().ToLower().Replace("book", "list"));
                        t_book_info.BookName = t_mc.Groups[4].Value.ToString();
                    }

                    t_mc = Regex.Match(t_html, "<div[^<>]*?class=[\'\"]*?b_content[\'\"]*?[^<>]*?>\\s*?<div[^<>]*?>\\s*?<p>\\s*?<a[^<>]*?>(.*?)</a>", RegexOptions.IgnoreCase | RegexOptions.Compiled);
                    if (t_mc != null && t_mc.Groups.Count > 0)
                    {
                   
                        t_book_info.BookDescription = NetSiteCatchManager.ReplaceContent(t_mc.Groups[1].Value.ToString());

                    }
                    t_mc=Regex.Match(t_html,"<li>授权级别：(.*?)</li>\\s*?<li>本周贵宾推荐：(\\d+)</li>.*?<li>本月被阅读数：(\\d+)</li>",RegexOptions.IgnoreCase|RegexOptions.Compiled);
                    if (t_mc != null && t_mc.Groups.Count > 0)
                    {
                        if (t_mc.Groups[1].Value.ToString().IndexOf("VIP") > -1)
                        {
                            t_book_info.IsVip = true;
                        }
                        int t_collect = 0;
                        if (!int.TryParse(t_mc.Groups[2].Value.ToString(), out t_collect))
                            t_collect = 0;

                        int t_click = 0;
                        if (!int.TryParse(t_mc.Groups[3].Value.ToString(), out t_click))
                            t_click = 0;

                        t_book_info.BookClick = t_click;
                        t_book_info.BookCollect = t_collect;
                    }
                    t_mc=Regex.Match(t_html,"<div[^<>]*?class=[\'\"]*?img[^<>]*?>\\s*?<span[^<>]*?>\\s*?</span>\\s*?<img[^<>]*?src=s*[\'\"]*([^\"\']*)[\'\"]*[^<>]*?>\\s*?</div>",RegexOptions.IgnoreCase|RegexOptions.Compiled);
                    if (t_mc != null && t_mc.Groups.Count > 0)
                    {
                        t_book_info.BookImageUrl = t_mc.Groups[1].Value.ToString();
                    }
                    t_mc=Regex.Match(t_html,"作者：\\s*?<[^<>]*?>(.*?)</a>",RegexOptions.IgnoreCase|RegexOptions.Compiled);
                    if (t_mc != null && t_mc.Groups.Count > 0)
                    {
                        t_book_info.BookAuthorName = t_mc.Groups[1].Value.ToString();
                    }
                    t_book_info.SiteId = (int)Consts.Site.yqk;

                }
            }
            catch (Exception ex)
            {
                LogHelper.Error("收集小说详情失败" + ex.ToString());
            }
           
        
            return t_book_info;
        }



        /// <summary>
        /// 收集某本书的所有章节
        /// </summary>
        /// <param name="i_chapter_list_url"></param>
        /// <returns></returns>
        public List<BookChapterInfo> CollectChapterList(string i_chapter_list_url)
        {
            List<BookChapterInfo> t_list_chapter = null;
            Uri t_url = new Uri(i_chapter_list_url);
            string t_reg_chapter="<li[^<>]*?>(.*?)<a[^<>]*?title=[\'\"]?[字]?[数]?[：]?([\\d+]*).*?更新日期：(\\d{4}-[\\d{2}|\\d{1}]+-[\\d{2}|\\d{1}]+\\s[\\d{2}|\\d{1}]+:[\\d{2}|\\d{1}]+:[\\d{2}|\\d{1}]+).*?[\'\"]*[\'\"]\\s*?[^<>]*?[^<>]*?href=s*[\'\"]*([^\"\']*)[\'\"]*[^<>]*?>(.*?)</a>\\s*?</li>";
            bool t_is_vip = false;
            DateTime t_update_time = DateTime.Now;
            try
            {
                string t_html = NetSiteCatchManager.ReadUrl(i_chapter_list_url, Encoding.UTF8);
                if (!string.IsNullOrEmpty(t_html))
                {
                    t_list_chapter = new List<BookChapterInfo>();
                   
                    MatchCollection t_ma_chapter = Regex.Matches(t_html, t_reg_chapter, RegexOptions.IgnoreCase | RegexOptions.Compiled);
                    if (t_ma_chapter != null)
                    {
                        foreach (Match t_mc in t_ma_chapter)
                        {

                            t_is_vip = false;
                            if (t_mc.Groups.Count >= 4)
                            {
                                BookChapterInfo t_book_chapter_info = new BookChapterInfo();

                                t_book_chapter_info.ChapterName = t_mc.Groups[5].Value.ToString();

                                if (!DateTime.TryParse(t_mc.Groups[3].Value.ToString(), out t_update_time))
                                    t_update_time = DateTime.Now;
                                t_book_chapter_info.UpdateTime = t_update_time;

                                if (!string.IsNullOrEmpty(t_mc.Groups[1].Value.ToString()))
                                {
                                    t_is_vip = true;
                                }
                                t_book_chapter_info.IsVip = t_is_vip;

                                t_book_chapter_info.ChapterUrl = string.Format("http://{0}{1}", t_url.Host, t_mc.Groups[4].Value.ToString());

                                t_book_chapter_info.SiteId = (int)Consts.Site.yqk;
                                t_list_chapter.Add(t_book_chapter_info);
                            }
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                LogHelper.Error(string.Format("收集小说:{0},章节失败:{1}", i_chapter_list_url, ex.ToString()));
            }
            //按跟新时间排序
            //if (t_list_chapter != null && t_list_chapter.Count > 0)
            //{
            //    t_list_chapter.Sort(delegate(BookChapterInfo t_a, BookChapterInfo t_b)
            //    {
            //        if (t_a.UpdateTime > t_b.UpdateTime)
            //            return 1;
            //        else if (t_a.UpdateTime == t_b.UpdateTime)
            //            return 0;
            //        return -1;
            //    });
            //}
            return t_list_chapter;
        }



        /// <summary>
        /// 收集章节内容
        /// </summary>
        /// <param name="i_chapter_content_url"></param>
        /// <returns></returns>
        public string CollectChapterContent(string i_chapter_content_url)
        {
            if (string.IsNullOrEmpty(i_chapter_content_url))
                return string.Empty;

         
            string t_reg_content = "document.write\\('(.*?)\'\\)";
            string t_chapter_content = string.Empty;
            try
            {
                string t_html = NetSiteCatchManager.ReadUrl(i_chapter_content_url, Encoding.GetEncoding("gb2312"));

                if (!string.IsNullOrEmpty(t_html))
                {

                    Match t_mc_content = Regex.Match(t_html, t_reg_content, RegexOptions.IgnoreCase | RegexOptions.Compiled);
                    if (t_mc_content != null && t_mc_content.Groups.Count > 0)
                    {
                        t_chapter_content = t_mc_content.Groups[1].Value.ToString();
                        t_chapter_content = NetSiteCatchManager.ReplaceContent(t_chapter_content);
                        t_chapter_content = string.Format("document.write('{0}');", t_chapter_content);
                    }
                }

                return t_chapter_content;
            }
            catch (Exception ex)
            {
                LogHelper.Error("获取章节内容失败" + ex.ToString());
                return string.Empty;
            }
        }



        private string ParseContent(string i_conent)
        {
            if (string.IsNullOrEmpty(i_conent))
                return string.Empty;

            //替换掉所有的Html标记
            string t_conent = Regex.Replace(i_conent, "<[^\u4e00-\u9fa5]>", "", RegexOptions.IgnoreCase | RegexOptions.Compiled).Trim();

            if (t_conent.Length > 250)
                return i_conent;
            return string.Empty;
        }

        /// <summary>
        /// 收集小说评论
        /// </summary>
        /// <param name="i_comment_url"></param>
        /// <returns></returns>
        public List<string> CollectReview(string i_comment_url)
        {
            List<string> t_comment_list=new List<string>();
            try
            {
                string  t_html = NetSiteCatchManager.ReadUrl(i_comment_url, Encoding.GetEncoding("gb2312"));

                if (!string.IsNullOrEmpty(t_html))
                {
                    MatchCollection t_ma = Regex.Matches(t_html, "<div[^<>]*?class=[\'\"]title[\'\"]?>.*?</div>\\s*?<div[^<>]*?class=[\'\"]?cont[\'\"]?\\s*?>(.*?)</div>", RegexOptions.IgnoreCase | RegexOptions.Compiled);

                    if (t_ma != null && t_ma.Count > 0)
                    {
                      
                        foreach (Match t_mc_review in t_ma)
                        {
                            string t_content = t_mc_review.Groups[1].Value.ToString();
                            t_content = Regex.Replace(t_content, "<.*?>.*?<.*?>", "", RegexOptions.IgnoreCase | RegexOptions.Compiled);
                            t_content = Regex.Replace(t_content, "<.*?>", "", RegexOptions.IgnoreCase | RegexOptions.Compiled);
                            t_content = Regex.Replace(t_content, "\\d+起点币", "", RegexOptions.IgnoreCase | RegexOptions.Compiled);
                            t_content = Regex.Replace(t_content, "起点中文网", "", RegexOptions.IgnoreCase | RegexOptions.Compiled);
                            t_content = Regex.Replace(t_content, "[a-zA-Z0-9]", "", RegexOptions.IgnoreCase | RegexOptions.Compiled);
                            if (string.IsNullOrEmpty(t_content) || t_content.Length < 10)
                                continue;
                           t_comment_list.Add(t_content);
                        }
                    }
                }

                return t_comment_list;
               
            }
            catch (Exception ex)
            {
                LogHelper.Error("获取评论失败" + ex.ToString());
                return null;
            }
        }
    }
}
